
package com.feib.soeasy.action;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;

import com.feib.soeasy.model.LabelValue;
import com.feib.soeasy.model.MenuItem;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.security.SoeasyInvocationSecurityMetadataSourceService;
import com.feib.soeasy.service.MenuItemService;


/**
 * @title (#)Soez9003ActionBean.java<br>
 * @description 權限功能設定<br>
 * @author Anson Tsai<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/16 create by Anson Tsai<br>
 */
public class Soez9003ActionBean extends BaseActionBean implements ValidationErrorHandler {

    /**
     * <code>serialVersionUID</code> 的註解
     */
    private static final long serialVersionUID = 1L;
    
    /**
     * 權限功能設定畫面
     */
    private static final String DEFAULT_JSP = "/WEB-INF/views/soez9003/soez9003.jsp";
    
    /**
     * 權限功能清單畫面
     */
    private static final String MENU_LIST_JSP = "/WEB-INF/views/soez9003/soez9003List.jsp";    
        
    /**
     * 權限功能維護畫面
     */
    private static final String AJAX_QUERY_MENU_JSP = "/WEB-INF/views/soez9003/menuItemList.jsp";    
    
    /**
     * 權限功能新增畫面
     */
    private static final String AJAX_ADD_MENU_JSP = "/WEB-INF/views/soez9003/menuItemAdd.jsp";
        
    /**
     * 權限功能新增結果畫面
     */
    private static final String AJAX_ADD_RESULT_JSP = "/WEB-INF/views/soez9003/menuItemAddResult.jsp";    
    
    /**
     * 權限功能更新結果畫面
     */
    private static final String AJAX_MODIFY_RESULT_JSP = "/WEB-INF/views/soez9003/menuItemModifyResult.jsp";    
        
    /**
     * 權限功能刪除結果畫面
     */
    private static final String AJAX_DELETE_RESULT_JSP = "/WEB-INF/views/soez9003/menuItemDeleteResult.jsp"; 
    
    /**
     * 程式類別
     */
    private String menuType;
    
    /**
     * 
     */
    private Long menuItemPk;
    
    /**
     * 程式代號
     */
    private String menuId;
    
    /**
     * 程式名稱
     */
    private String actionName;
    
    /**
     * 查詢結果
     */
    private PaginatedList<MenuItem> queryResult;
    
    /**
     * 
     */
    private MenuItem resultMenuItem;
    
    /**
     * 程式群組選單
     */
    private List<LabelValue> parentMenuItemOptions;
    
    /**
     * 新增處理結果
     */
    private boolean inserted;
    
    /**
     * 更新處理結果
     */
    private boolean modifyed;
    
    /**
     * 刪除處理結果
     */
    private boolean deleteed;
    
    @SpringBean("menuItemService")
    private MenuItemService menuItemService; 
    
    @SpringBean("soeasySecurityMetadataSource")
    private SoeasyInvocationSecurityMetadataSourceService invocationSecurityMetadataSourceService;
    
    @DontBind
    @DefaultHandler
    public Resolution doDefault() {
        logger.debug("Enter doDefault mehtod.");
        return new ForwardResolution(DEFAULT_JSP);
    }

    /**
     * 查詢
     * @return
     */
    public Resolution doAjaxQuery()
    {
        logger.debug("Enter doAjaxQuery mehtod.");
        try {
            //呼叫服務查詢
            queryResult = menuItemService.queryByConditions(menuType, menuId, actionName, queryResult);
        }
        catch (Exception e) {
            logger.error("權限功能設定作業查詢錯誤(Soez9003ActionBean.doAjaxQuery):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }        
        return new ForwardResolution(MENU_LIST_JSP);
    }
        
    /**
     * 新增權限功能資料
     * @return
     */
    public Resolution doAjaxAddMenuItem()
    {
        logger.debug("Enter doAjaxAddMenuItem mehtod.");
        try {
            resultMenuItem = new MenuItem();
        }
        catch (Exception e) {
            logger.error("權限功能設定作業-權限功能新增錯誤(doAjaxAddMenuItem):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxAddMenuItem.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(AJAX_ADD_MENU_JSP);
    }
    
    /**
     * 查詢權限功能資料
     * @return
     */
    public Resolution doAjaxQueryMenuItem()
    {
        logger.debug("Enter doAjaxQueryMenuItem mehtod.");
        try {

            //呼叫服務查詢
            resultMenuItem = 
                menuItemService.getMenuItemByPk(menuItemPk);
        }
        catch (Exception e) {
            logger.error("權限功能設定作業-權限功能查詢錯誤(doAjaxQueryMenuItem):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQueryMenuItem.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(AJAX_QUERY_MENU_JSP);
    }
    
    /**
     * 
     * @return
     */
    public Resolution doMenuItemDeleteAction()
    {        
        logger.debug("Enter doMenuItemDeleteAction mehtod.");
        deleteed = false;
        try {
            logger.debug("delete MenuItem Object , Pk : " + resultMenuItem.getMenuItemPk());
            
            menuItemService.delete(menuItemService.getMenuItemByPk(resultMenuItem.getMenuItemPk()));
            
            // 更新 Spring Security 設定
            invocationSecurityMetadataSourceService.setToReload();
            
            deleteed = true;
        }
        catch (Exception e) {
            logger.error("權限功能設定作業-權限功能刪除錯誤(Soez9003ActionBean.doMenuItemDeleteAction):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doMenuItemDeleteAction.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }     
        return new ForwardResolution(AJAX_DELETE_RESULT_JSP);
    }
    
    /**
     * 
     * @return
     */
    public Resolution doMenuItemAddAction()
    {
        logger.debug("Enter doMenuItemAddAction mehtod.");
        inserted = false;
        try {
            if (null != resultMenuItem.getParentMenuItem())
            {
                logger.debug("Add menuItem.parentMenuItem Object , Pk : " + resultMenuItem.getParentMenuItem().getMenuItemPk());
                MenuItem p = menuItemService.getMenuItemByPk(resultMenuItem.getParentMenuItem().getMenuItemPk());
                resultMenuItem.setParentMenuItem(p);
            }                
            else
            {
                logger.debug("Add menuItem.parentMenuItem Object is null");
            }                
            logger.debug("Add MenuItem Object , MenuId : " + resultMenuItem.getMenuId());
            logger.debug("Add MenuItem Object , ActionName : " + resultMenuItem.getActionName());
            logger.debug("Add MenuItem Object , ActionBean : " + resultMenuItem.getActionBean());
            logger.debug("Add MenuItem Object , ActionPage : " + resultMenuItem.getActionPage());
            logger.debug("Add MenuItem Object , orderNo : " + resultMenuItem.getOrderNo());
            logger.debug("Add MenuItem Object , Enabled : " + resultMenuItem.getEnabled());
            
            menuItemService.save(resultMenuItem);
            
            // 更新 Spring Security 設定
            invocationSecurityMetadataSourceService.setToReload();
            
            inserted = true;
        }
        catch (Exception e) {
            logger.error("權限功能設定作業-權限功能新增錯誤(Soez9003ActionBean.doMenuItemAddAction):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doMenuItemAddAction.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }
        return new ForwardResolution(AJAX_ADD_RESULT_JSP);
    }
    
    /**
     * 
     * @return
     */
    public Resolution doMenuItemModifyAction()
    {
        logger.debug("Enter doMenuItemModifyAction mehtod.");
        modifyed = false;
        try {
            logger.debug("modify MenuItem Object , Pk : " + resultMenuItem.getMenuItemPk());
            MenuItem menu = menuItemService.getMenuItemByPk(resultMenuItem.getMenuItemPk());
            if (null != resultMenuItem.getParentMenuItem())
            {
                logger.debug("modify menuItem.parentMenuItem Object , Pk : " + resultMenuItem.getParentMenuItem().getMenuItemPk());
                MenuItem p = menuItemService.getMenuItemByPk(resultMenuItem.getParentMenuItem().getMenuItemPk());
                menu.setParentMenuItem(p);
            }                
            else
            {
                logger.debug("modify menuItem.parentMenuItem Object is null");
            }                
            logger.debug("modify MenuItem Object , MenuId : " + resultMenuItem.getMenuId());
            logger.debug("modify MenuItem Object , ActionName : " + resultMenuItem.getActionName());
            logger.debug("modify MenuItem Object , ActionBean : " + resultMenuItem.getActionBean());
            logger.debug("modify MenuItem Object , ActionPage : " + resultMenuItem.getActionPage());
            logger.debug("modify MenuItem Object , orderNo : " + resultMenuItem.getOrderNo());
            logger.debug("modify MenuItem Object , Enabled : " + resultMenuItem.getEnabled());
            logger.debug("modify MenuItem Object , School : " + resultMenuItem.getSchool());
            logger.debug("modify MenuItem Object , Committee : " + resultMenuItem.getCommittee());            
            
            menu.setMenuId(resultMenuItem.getMenuId());
            menu.setActionName(resultMenuItem.getActionName());
            menu.setActionBean(resultMenuItem.getActionBean());
            menu.setActionPage(resultMenuItem.getActionPage());
            menu.setOrderNo(resultMenuItem.getOrderNo());
            menu.setEnabled(resultMenuItem.getEnabled());
            menu.setSchool(resultMenuItem.getSchool());
            menu.setCommittee(resultMenuItem.getCommittee());           
            
            menuItemService.save(menu);
            
            // 更新 Spring Security 設定
            invocationSecurityMetadataSourceService.setToReload();
            
            modifyed = true;
        }
        catch (Exception e) {
            logger.error("權限功能設定作業-權限功能更新錯誤(Soez9003ActionBean.doMenuItemModifyAction):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doMenuItemModifyAction.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }
        return new ForwardResolution(AJAX_MODIFY_RESULT_JSP);
    }
    
    /**
     * @return 傳回 menuType。
     */
    public String getMenuType() {
        return menuType;
    }
    
    /**
     * @param menuType 要設定的 menuType。
     */
    public void setMenuType(String menuType) {
        this.menuType = menuType;
    }
    
    /**
     * @return 傳回 menuId。
     */
    public String getMenuId() {
        return menuId;
    }
   
    /**
     * @param menuId 要設定的 menuId。
     */
    public void setMenuId(String menuId) {
        this.menuId = menuId;
    }
    
    /**
     * @return 傳回 actionName。
     */
    public String getActionName() {
        return actionName;
    }
    
    /**
     * @param actionName 要設定的 actionName。
     */
    public void setActionName(String actionName) {
        this.actionName = actionName;
    }
    
    /**
     * @return 傳回 queryResult。
     */
    public PaginatedList<MenuItem> getQueryResult() {
        return queryResult;
    }
    
    /**
     * @param queryResult 要設定的 queryResult。
     */
    public void setQueryResult(PaginatedList<MenuItem> queryResult) {
        this.queryResult = queryResult;
    }
   
    /**
     * @return 傳回 menuItemPk。
     */
    public Long getMenuItemPk() {
        return menuItemPk;
    }
    
    /**
     * @param menuItemPk 要設定的 menuItemPk。
     */
    public void setMenuItemPk(Long menuItemPk) {
        this.menuItemPk = menuItemPk;
    }
    
    /**
     * @return 傳回 resultMenuItem。
     */
    public MenuItem getResultMenuItem() {
        return resultMenuItem;
    }
    
    /**
     * @param resultMenuItem 要設定的 resultMenuItem。
     */
    public void setResultMenuItem(MenuItem resultMenuItem) {
        this.resultMenuItem = resultMenuItem;
    }
    
    /**
     * @return 傳回 parentMenuItemOptions。
     */
    public List<LabelValue> getParentMenuItemOptions() {
        if (null == this.parentMenuItemOptions)
        {
            this.parentMenuItemOptions = this.menuItemService.findLabelValueByParentMenuItem();
            if (null == this.parentMenuItemOptions)
                this.parentMenuItemOptions = new ArrayList<LabelValue>();
        }
        return parentMenuItemOptions;
    }
    
    /**
     * @param parentMenuItemOptions 要設定的 parentMenuItemOptions。
     */
    public void setParentMenuItemOptions(List<LabelValue> parentMenuItemOptions) {
        this.parentMenuItemOptions = parentMenuItemOptions;
    }

    
    /**
     * @return 傳回 modifyed。
     */
    public boolean isModifyed() {
        return modifyed;
    }

    
    /**
     * @param modifyed 要設定的 modifyed。
     */
    public void setModifyed(boolean modifyed) {
        this.modifyed = modifyed;
    }

    
    /**
     * @return 傳回 deleteed。
     */
    public boolean isDeleteed() {
        return deleteed;
    }

    
    /**
     * @param deleteed 要設定的 deleteed。
     */
    public void setDeleteed(boolean deleteed) {
        this.deleteed = deleteed;
    }

    
    /**
     * @return 傳回 inserted。
     */
    public boolean isInserted() {
        return inserted;
    }

    
    /**
     * @param inserted 要設定的 inserted。
     */
    public void setInserted(boolean inserted) {
        this.inserted = inserted;
    }       
    
}
